package com.sdp.wx.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.right.cron.ScheduledOfTask;
import com.sdp.wx.db.wx_emp.entity.WxEmp;
import com.sdp.wx.db.wx_emp.service.IWxEmpService;
import com.sdp.wx.db.wx_emp_work.entity.WxEmpWork;
import com.sdp.wx.db.wx_emp_work.service.IWxEmpWorkService;
import com.sdp.wx.db.wx_emp_work_attendance.entity.WxEmpWorkAttendance;
import com.sdp.wx.db.wx_emp_work_attendance.service.IWxEmpWorkAttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class WxEmpWorkTask implements ScheduledOfTask {

    @Autowired
    private IWxEmpWorkAttendanceService iWxEmpWorkAttendanceService;
    @Autowired
    private IWxEmpService iWxEmpService;
    @Autowired
    private IWxEmpWorkService iWxEmpWorkService;
    @Autowired
    private Query query;
    @Override
    public void execute() {
        // 每天凌晨生成最新的记录
        List<WxEmpWorkAttendance> wxEmpWorkAttendanceList= iWxEmpWorkAttendanceService.list();
        if(wxEmpWorkAttendanceList.size()>0){
            for (WxEmpWorkAttendance wxEmpWorkAttendance:wxEmpWorkAttendanceList){
                if(!StringUtils.isEmpty(wxEmpWorkAttendance.getWorkOrgNo())){
                    QueryWrapper<WxEmp> queryWrapper2=new QueryWrapper<>();
                    List<String> orgNo= Arrays.asList(wxEmpWorkAttendance.getWorkOrgNo().split(","));
                    queryWrapper2.lambda().in(WxEmp::getOrgNo,orgNo);
                    List<WxEmp>  wxEmpList= iWxEmpService.list(queryWrapper2);
                    if(wxEmpList.size()>0){
                        List<WxEmpWork> wxEmpWorkList=new ArrayList<>();
                        for(WxEmp wxEmp:wxEmpList){
                            WxEmpWork wxEmpWork=new WxEmpWork();
                            wxEmpWork.setWorkName(wxEmpWorkAttendance.getWorkName());
                            wxEmpWork.setWorkEmpNo(wxEmp.getEmpNo());
                            wxEmpWork.setWorkOrgNo(wxEmp.getOrgNo());
                            wxEmpWork.setCreateTime(LocalDateTime.now());
                            wxEmpWork.setWorkEmpName(wxEmp.getEmpName());
                            wxEmpWork.setWorkAttendanceId(wxEmpWorkAttendance.getKeyId());
                            wxEmpWorkList.add(wxEmpWork);
                        }
                        iWxEmpWorkService.saveBatch(wxEmpWorkList);
                    }
                }
            }
        }

    }
}
